package com.modelscope.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.modelscope.ai.domain.entity.SubscribeArticle;
import com.modelscope.ai.domain.entity.Visit;
import com.modelscope.ai.domain.vo.VisitVo;
import com.modelscope.ai.mapper.VisitMapper;
import com.modelscope.ai.service.SubscribeArticleService;
import com.modelscope.ai.mapper.SubscribeArticleMapper;
import com.modelscope.ai.utils.AssertUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【subscribe_article】的数据库操作Service实现
 * @createDate 2024-03-17 21:46:13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SubscribeArticleServiceImpl extends ServiceImpl<SubscribeArticleMapper, SubscribeArticle>
        implements SubscribeArticleService {

    final VisitMapper visitMapper;

    @Override
    public List<VisitVo> querySubscribeArticle(Long uid) {
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSubscribeArticle(SubscribeArticle subscribeArticle) {

        log.info("{}",subscribeArticle);
        AssertUtil.isNotEmpty(subscribeArticle.getArticleId(), "新增id不应该存在");
        // 或者已经存在了
        SubscribeArticle one = lambdaQuery().eq(SubscribeArticle::getArticleId, subscribeArticle.getArticleId())
                .eq(SubscribeArticle::getUid, subscribeArticle.getUid()).one();
        AssertUtil.isEmpty(one,"已经添加了合集");
        getBaseMapper().insert(subscribeArticle);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSubscribeArticle(List<Long> ids) {
//        List<SubscribeArticle> list=lambdaQuery().getBaseMapper().selectBatchIds(ids);

//        List<Long> l=list.stream().map(SubscribeArticle::getId).toList();
        log.info("删除的ids:{}",ids);
        visitMapper.delete(new LambdaQueryWrapper<Visit>().in(Visit::getPid, ids));
        // 第二步 删除 所有
        removeBatchByIds(ids);
    }
}




